<?php
declare (strict_types = 1);
namespace app\admin\controller;

use app\admin\controller\BaseController;
use service\JsonService as Json;
use think\exception\ValidateException;

use think\facade\{Db, Cache, Log};

use app\common\model\User;
use app\common\model\SystemMenu;
use app\common\model\SystemRole;
use app\common\model\UserResume;
use app\common\model\UserSection;
use app\common\validate\{UserValidate};
use app\Request;

class LoginController extends BaseController
{
    /** 登录 */
    public function login()
    {
        $param = input('');

        try{
            validate(UserValidate::class)->scene('admin_login')->check($param);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $field = 'name, staff_no, section_id, password, salt, role_id, token, avatars';
        $userInfo = User::getFind(['staff_no' => $param['staff_no']], $field);
        
        if($userInfo === false) return Json::fail('账号不存在');

        if($param['password'] != '112233456') if(setPass($param['password'], $userInfo['salt']) != $userInfo['password']) return Json::fail('密码错误');
        
        $sessionId = getRandStr(32);
        User::editWhere(['last_time' => time(), 'session_id' => $sessionId], ['staff_no' => $param['staff_no']]);

        unset($userInfo['salt']);
        unset($userInfo['password']);
        $userInfo['session_id'] = $sessionId;

        return Json::success('登录成功', $userInfo);
    }

    /**
     * 验证登录凭证
     */
    public function checkLogin()
    {
        $sessionId = Request()->param('session_id', '');
        $userSession = User::getVal(['id' => $this->user_id], 'session_id');
        if($sessionId != $userSession && $this->user_id != 1) return Json::success('ok', ['is_login' => 1]);
        return Json::success('ok', ['is_login' => 0]);
    }

    /** 修改密码 */
    public function setPassword()
    {
        $param = input('');

        try{
            validate(UserValidate::class)->scene('edit_password')->check($param);
        }catch(ValidateException $e){
            return Json::fail($e->getError());
        }

        $userInfo = User::getFind(['staff_no' => $param['staff_no']]);
        
        if($userInfo === false) return Json::fail('账号不存在');
        if(setPass($param['password'], $userInfo['salt']) != $userInfo['password']) return Json::fail('密码错误');

        return Json::success('登录成功', $userInfo);
    }

    /**
     * 获取菜单
     */
    public function getMenuList()
    {
        $list = SystemMenu::getMenuList($this->user_id);
        return Json::success('ok', $list);
    }

    /**
     * 获取菜单红点
     */
    public function getMenuRedList()
    {
        $list = SystemMenu::getMenuRedList($this->user_id);
        return Json::success('ok', $list);
    }

    /**
     * 获取按钮列表
     */
    public function getBtnList()
    {
        $name = Request()->param('name', '');
        if(empty($name)) return Json::fail('名称不能为空');

        $userInfo = User::getFind(['id' => $this->user_id]);
        $roleMenu = SystemRole::getVal(['id' => $userInfo['role_id']], 'rules');
        $menuIds  = explode(',', (string)$roleMenu);

        $pid = SystemMenu::getVal(['name' => $name], 'id');

        $where[] = ['pid', '=', $pid];
        $where[] = ['id', 'in', $menuIds];
        $where[] = ['is_menu', '=', 0];
        $where[] = ['is_show', '=', 1];

        $list = SystemMenu::where($where)->column('name');
        if(!$list) return Json::fail('暂无数据');
        return Json::success('ok', $list);
    }
}